/**
 * 角色的公共方法
 * @version V1.0
 */
var Role = {
	/**
	 * 角色联想框
	 */
	suggestRoleEditOpt : function(){
		return {
			url : "services/manage/security/roleManage/list/page/80/1",
			fieldMapper : {
				name : $i18n("ccay.security.label.roleName"),
				id : $i18n("ccay.common.label.desc")
			},
			displayField : "name",
			valueField : "id"
		};
	},
	/**
	 * 递归获取条目 
	 */
   	permissionList : function(children,parentEntry){
   		var result = [];
   		var opt = [];
		for(var i=0; i<children.length; i++){
			if(children[i].permissionString && children[i].checked){
				opt.push(children[i].name);
			}else{
				var path = [];
				for(var k=0;k<parentEntry.path.length;k++){
					path.push(parentEntry.path[k])
				}
				path.push(children[i].name);
				var entry = {
					path : path,
					opt : parentEntry.opt
				};
				var childrenEntries = Role.permissionList(children[i].children,entry)
				for(var j=0;j<childrenEntries.length;j++){
					result.push(childrenEntries[j]);
				}
			}
		}
		if(opt.length != 0){
			parentEntry.opt = opt;
			result.push(parentEntry);
		}
		return result;
   },
   	/**
	 * 根据资源类型 加载权限功能点树
	 */
   initHasPermissionList : function (resources,divId){
   		//递归获取条目 
   		var permissionEntries = Role.permissionList(resources,{path:[],opt:[]});
   		//按照资源名称排序
   		permissionEntries.sort(function(a,b){return a.path.join("") >  b.path.join("");});
   		//构建显示HTML
     	var liStr = "<li>"+$i18n("ccay.role.message.currentUserPermissions")+"</li>";
     	for(var i=0;i<permissionEntries.length;i++){
     		liStr += "<li class=\"bold\">"+permissionEntries[i]["path"].join("->").encode()+"</li>";
     		liStr += "<ul><li>&nbsp;&nbsp;&nbsp;"+permissionEntries[i]["opt"].join(" ").encode()+"</li></ul>";
     	}
     	//显示
     	$(divId).html(liStr);	
   }
}